#include "DmMainAfx.h"

#include "./StretchTable.h"
#include "./StretchKernel.h"

extern DWORD g_dwStretchKernel;
DWORD StretchTable[(2<<8)+1];

static BYTE _ColorTable[256*3];
const BYTE* ColorTable = &_ColorTable[256];

#define STRETCH_CUBIC	(DWORD)0x00000000
#define STRETCH_BSPLINE	(DWORD)0x00000001
#define STRETCH_SINC	(DWORD)0x00000002

void InitStretchTable()
{
	xCPUInfo::CheckCPUID();
	if (xCPUInfo::IsSupportMMX())
	{
        WORD t;
	    if (g_dwStretchKernel == STRETCH_CUBIC)
	    {
		    for (int i = 0; i <= (2 << 8); ++i)
            {
				t = (WORD)(0.5 + (1 << 14) * KernelCubic(i * (1.0 / (256))));
			    StretchTable[i] = t | (((DWORD)t) << 16);
            }
	    }
	    else if (g_dwStretchKernel == STRETCH_BSPLINE)
	    {
		    for (int i = 0; i <= (2 << 8); ++i)
            {
			    t = (WORD)(0.5 + (1 << 14) * KernelBSpline(i * (1.0 / (256))));
                StretchTable[i] = t | (((DWORD)t) << 16);
            }
	    }
	    else
	    {
		    for (int i = 0; i <= (2 << 8); ++i)
            {
			    t = (WORD)(0.5 + (1 << 14) * KernelSinc(i * (1.0 / (256))));
                StretchTable[i] = t | (((DWORD)t) << 16);
            }
	    }
	}
    else
    {
	    if (g_dwStretchKernel == STRETCH_CUBIC)
	    {
		    for (int i = 0; i <= (2 << 8); ++i)
			    StretchTable[i] = (DWORD)(0.5 + 256 * KernelCubic(i * (1.0 / (256))));
	    }
	    else if (g_dwStretchKernel == STRETCH_BSPLINE)
	    {
		    for (int i = 0; i <= (2 << 8); ++i)
			    StretchTable[i] = (DWORD)(0.5 + 256 * KernelBSpline(i * (1.0 / (256))));
	    }
	    else
	    {
		    for (int i = 0; i <= (2 <<8 ); ++i)
			    StretchTable[i] = (DWORD)(0.5 + 256 * KernelSinc(i * (1.0 / (256))));
	    }
    }
}

void InitColorTable()
{
	for (int i = 0; i < 256 * 3; ++i)
		_ColorTable[i] = (BYTE)((int)min(max(i - 256, 0), 255));
}